package belf.migrate.engine.dao;

import belf.migrate.api.common.JobMode;
import belf.migrate.api.taskconf.TaskConf;
import belf.migrate.api.taskconf.Task;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 从数据库中查询控制台配置的{@link Task}信息
 */
@Slf4j
public class TaskDAO extends BaseDAO<Task> {

    @Override
    public void save(Task task) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            String sql = "INSERT INTO migration_task" +
                    "(task_name,task_desc,owner,create_date,task_conf_id,task_type,job_mode,task_policy)" +
                    " values(?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1, task.getName());
            ps.setString(2, task.getDesc());
            ps.setString(3, task.getOwner());
            ps.setDate(4, new java.sql.Date(task.getCreateDate().getTime()));
            ps.setLong(5, task.getTaskConf().getId());
            ps.setString(6, task.getTaskType().name());
            ps.setString(7, task.getJobMode().name());
            ps.setString(8, task.getTaskPolicy().name());
            ps.executeUpdate();
            ps.close();
            log.info("Save Task to db: {}", task.getName());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) ps.close();
                if (null != connection) connection.close();
            } catch (SQLException e) {}
        }
    }

    @Override
    public Task queryById(long id) {
        Task task = null;
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            String sql = "SELECT * FROM migration_task WHERE task_id=?";
            ps = connection.prepareStatement(sql);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                TaskConf taskConf = new TaskConfDAO().queryById(rs.getInt("task_conf_id"));
                task = new Task(rs.getInt("task_id"),
                        rs.getString("task_name"),
                        rs.getString("task_desc"),
                        rs.getString("owner"),
                        rs.getDate("create_date"),
                        taskConf,
                        Enum.valueOf(Task.TaskType.class, rs.getString("task_type")),
                        null,
                        Enum.valueOf(JobMode.class, rs.getString("job_mode")),
                        Enum.valueOf(Task.TaskPolicy.class, rs.getString("task_policy")));

            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) ps.close();
                if (null != connection) connection.close();
            } catch (SQLException e) {}
        }
        return task;
    }

    @Override
    public void deleteById(long id) {
        log.info("Delete Task by id: {}", id);
        String sql = "DELETE FROM migration_task where task_id=" + id;
        executeSql(sql);
    }
}
